// pages/spareSeat/spareSeat.js
const db = wx.cloud.database()
Page({

  data: {
    loading: true,
    floor: '二楼',
    tab: ["二楼", "三楼", "四楼", "五楼"],
    seat_form:[],
    appointment: []
  },
  onChange(event) {
    
    this.setData({
        floor:event.detail.title
    })
    this.onLoad()
  },

   onLoad: async function (options) {
     this.setData({loading: true})
    //获取所有座位信息
    let count_seat = await db.collection("seat_position_form").count()
    count_seat = count_seat.total
    let all_seat = []
    for (let i = 0; i < count_seat; i+= 20){
      let  list_seat = await db.collection("seat_position_form").where({
        floor: this.data.floor
      }).skip(i).get()
      all_seat = all_seat.concat(list_seat.data)
    }
   //获取所有预约信息
    let count_app = await db.collection("appointment_time").count()
    count_app = count_app.total
    let all_app = []
    for (let i = 0; i < count_app; i += 20){
      let list_app =  await db.collection("appointment_time").where({
        floor: this.data.floor
        }).skip(i).get()
      all_app = all_app.concat(list_app.data)
    }
    let seat_list = all_seat
    let app_list = all_app
    let seat_app_list = []
    let s = new Date();
    let e = new Date();
    let appointment = []
    for (let i = 0; i < seat_list.length; i++) {
      seat_app_list = []
      for (let j = 0; j < app_list.length; j++) {
        if (seat_list[i].position === app_list[j].position) {
          seat_app_list.push(app_list[j])
        }
      }
      let status = this.time_compare(s, e, seat_app_list)
      seat_list[i].status = status
      appointment.push(seat_list[i])
    }
    this.setData({
      appointment: appointment,
      loading:false
    })

  },
  time_compare(s, e, list) {
    var status = true
    var date_s
    var date_e
    for (let i = 0; i < list.length; i++) {
      date_s = new Date(list[i].start_time)
      date_e = new Date(list[i].end_time)
      if ((s > date_s && s < date_e) || (e > date_s && e < date_e) || (s <= date_s && e >= date_e)) {
        status = false
      }
    }
    return status
  },
  onShow: function () {

  },
  onShareAppMessage: function () {

  }
})